using System;
using System.Data;
using System.Configuration; 

namespace UploadData
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ExcelDataProvider excelProvider = new ExcelDataProvider();
                UploadSqlServerData sqlProvider = new UploadSqlServerData();                
                FileProvider fileProvider = new FileProvider();
                
                fileProvider.FolderPath = 
                ConfigurationManager.AppSettings[
                "ExcelFile"].ToString();
                sqlProvider.ConnectionString = 
                ConfigurationManager.AppSettings[
                "RecruitVietnam"].ToString(); 
                
                foreach(string fileName in fileProvider.GetFileNames())
                {
                    excelProvider.ExcelPathFile = fileName;
                    Console.WriteLine(fileName);  
                    DataTable dataTable =
                    excelProvider.GetDataFromExcel("JobPostings");
                    
                    if(dataTable != null)
                    {
                        if (sqlProvider.UploadDataIntoTable(dataTable))
                            Console.WriteLine("Uploading {0} records",
                            sqlProvider.UploadedRecords);
                    }
                    if (excelProvider.ErrorMessage != "")
                        Console.WriteLine("GetDataFromExcel::"
                        + excelProvider.ErrorMessage); 
                    else
                    {
                        fileProvider.DeleteFile(fileName);
                        Console.WriteLine("Completed");
                    }
                     
                }
                Console.ReadLine();
            }
            catch(Exception ex)
            {
                WriteErrorToFile(ex);
            }
             
        }
        
        static void WriteErrorToFile(Exception ex)
        {
            try
            {
                LogTrace.LogTraceProvider logTrace = 
                new LogTrace.LogTraceProvider();
                logTrace.WriteErrorToLogFile(ex.Message);
                Console.WriteLine(ex.Message);
            }
            catch  
            {
                 
            }
        }
    }
}
